<?php
/**
 * @description : choose a plan for user.
 * @author      : stephen.mo
 * @date        : 2010/09/02
 */
require_once "TVB/OAuth/common.inc.php";
#ini_set('display_errors','on');
$current_page = OAuthConfig::PAGE_PLAN_CHOOSE;
$pukey        = (isset($_REQUEST['pukey'])) ? $_REQUEST['pukey'] : NULL;
$model        = (isset($_REQUEST['model'])) ? $_REQUEST['model'] : NULL;
$access_token = (isset($_REQUEST['oauth_token'])) ? $_REQUEST['oauth_token'] : NULL;
$lang         = (isset($_REQUEST['lang'])) ? OAuthUtils::chkLang($_REQUEST['lang']) : OAuthConfig::DEFAULT_SUPPORT_LANG;
$login_id     = (isset($_REQUEST['login_id'])) ? $_REQUEST['login_id'] : NULL;
$call_back    = (isset($_REQUEST['call_back'])) ? $_REQUEST['call_back'] : NULL;
$reback_url   = (isset($_REQUEST['reback_url'])) ? $_REQUEST['reback_url'] : NULL;
$login_id     = (!empty($login_id)) ? $login_id : OAuthUtils::getDefaultLoginId($login_id);

//setting vars
$params = array();
$params['oauth_token']  = $access_token;
$params['lang']         = $lang;
$params['login_id']     = $login_id;
$params['call_back']    = urlencode($call_back);
$params['reback_url']   = urlencode($reback_url);

//message url ,when have some need to show.
$base_server_path = OAuthConfig::OAuthDomainUrl($_SERVER);
$base_back_url    = OAuthConfig::OAuthGetBackUrl($_SERVER);
$reset_page_url   = OAuthConfig::OAuthGetRestUrl($_SERVER);
$oauth_token_str  = (!empty($access_token)) ? '&oauth_token='.$access_token : NULL;
$login_id_str     = (!empty($login_id)) ? '&login_id='.$login_id : NULL; 
$header_url       = $base_server_path.OAuthConfig::DEFAULT_HEADER_URL;
$message_url      = $base_server_path.OAuthConfig::DEFAULT_SHOW_MESSAGE_URL;
$plan_signin_url  = $base_server_path.OAuthConfig::DEFAULT_QUICKLY_LOGIN;
$jqtouch_url      = $base_server_path.OAuthModelConfig::DEFAULT_MOBILE_TEMPLATE;

//verify the oauth token.
if (OAuthModelConfig::IS_NEED_CHECK_OAUTHTOKEN) {
    $ret = OAuthUtils::verifyAccessToken($access_token);
    if (!$ret || !$ret['flag']) {
        $other_msg = array('call_back'=>(isset($call_back) ? urlencode($call_back) : urlencode($reset_page_url)) );
        OAuthUtils::toMessageUrl($message_url,OAuthModelConfig::M_PAGE_OF_ERROR,OAuthModelConfig::DEFAULT_CONST_FALIURE,$lang,array(1005,1006),$other_msg);
    }
    //try to get login id from oauth token vars.
    if (empty($login_id)) {
        $user = unserialize($ret['data']['data']);
        $login_id = (!empty($user)) ? OAuthUtils::getLoginIdByEmail($user->user) : NULL;
    }
}

//get ptoken & pukey & verify it.
$ptoken = OAuthUtils::getPToken();
if (!empty($pukey) && !empty($ptoken)) {
    $pukey = OAuthUtils::reGenPUKey($pukey,$ptoken);
}
else {
    OAuthUtils::delPToken();
    OAuthUtils::redirectTo('restart_url',$params);
    exit;
}

//check user is logined?
$is_logined     = FALSE;
if (OAuthUtils::chkIsLogin($login_id,$access_token)) {
    $login_id   = OAuthUtils::getDefaultLoginId($login_id);
    $is_logined = (!empty($login_id)) ? TRUE : FALSE;
}

//check user can buy plan.
if (!empty($login_id)) {
    $rev = OAuthUtils::chkIfUserCanBuyPlan($login_id);
    if ((isset($rev) && $rev==1)) {
        $tmp_confirm_url = (isset($call_back)) ? $call_back : '#';
        $other_msg = array('login_id'=>$login_id,'call_back'=>urlencode($tmp_confirm_url));
        OAuthUtils::delPToken();
        OAuthUtils::toMessageUrl($message_url,OAuthModelConfig::M_PAGE_OF_PROMPT,OAuthModelConfig::DEFAULT_CONST_FALIURE,$lang,array(1031),$other_msg);
        exit;
    }
}

//call API get the package list.
$package_list = array();
$package_list = OAuthUtils::getPackageListByModel($lang);
$action_url   = $base_server_path.OAuthConfig::DEFAULT_INFORMATION_URL.'?call_back='.urlencode($call_back).'&reback_url='.urlencode($reback_url);
$back_url     = $base_server_path.OAuthConfig::DEFAULT_CMODEL_URL.'?call_back='.urlencode($call_back).
                '&reback_url='.urlencode($reback_url).$oauth_token_str.$login_id_str.'&lang='.$lang.'&pukey='.$pukey;

//get form data.
$fdata = array();
$tmpData      = OAuthUtils::getFormData($pukey,$ptoken);
if ($tmpData->retFlag==1) $tmpData = $tmpData->data;
$last_page    = (isset($tmpData->current_page)) ? $tmpData->current_page : NULL;
$back_url_top = (isset($tmpData->m_step_back_I)&&!empty($tmpData->m_step_back_I)) ? $tmpData->m_step_back_I.'&pukey='.$pukey : $base_back_url.'&pukey='.$pukey;

//allow to access this page.
$allow_pages = array(
    OAuthConfig::PAGE_PLAN_CMODEL,
    OAuthConfig::PAGE_PLAN_INFORMATION,
    OAuthConfig::PAGE_PLAN_CONFIRM,
);
if (!in_array($last_page,$allow_pages)) {
    OAuthUtils::delPToken();
    OAuthUtils::redirectTo('restart_url',$params);
}

//set data to memcache.
$fdata['m_login_id']      = (isset($tmpData->m_login_id)) ? $tmpData->m_login_id : $login_id;
$fdata['m_call_back']     = (isset($tmpData->m_call_back)) ? $tmpData->m_call_back : $call_back;
$fdata['m_lang']          = (isset($tmpData->m_lang)) ? $tmpData->m_lang : $lang;
$fdata['m_reback_url']    = (isset($tmpData->m_reback_url)) ? $tmpData->m_reback_url : $reback_url;
$fdata['p_bill_address']  = (isset($tmpData->p_bill_address)) ? $tmpData->p_bill_address : '';
$fdata['p_lang']          = (isset($tmpData->p_lang)) ? $tmpData->p_lang : '';
$fdata['m_step_back_I']   = (isset($tmpData->m_step_back_I)) ? $tmpData->m_step_back_I : '';
$fdata['m_step_back_II']  = $base_back_url;
$fdata['m_model']         = $model;
$fdata['current_page']    = $current_page;

//save data to mc.
$ret = OAuthUtils::setFormData($pukey,$ptoken,$fdata);
if (!$ret) {
    //not success need to retry it.
    $retry_url = OAuthConfig::OAuthGetBuyPlanFirst($params);
    $other_msg = array('call_back'=>urlencode($retry_url));
    OAuthUtils::toMessageUrl($message_url,OAuthModelConfig::M_PAGE_OF_PROMPT,OAuthModelConfig::DEFAULT_CONST_FALIURE,$lang,array(1000),$other_msg);
    exit;
}

//page info vars
$payment_buttons  = OPI::$payment_button_info;
$choose_plan_page = OPI::$choose_plan_info;
$submit_get_url   = $action_url.$oauth_token_str.$login_id_str.'&lang='.$lang.'&pukey='.$pukey;
$call_back_str    = (isset($call_back) && !empty($call_back)) ? '&call_back='.urlencode($call_back) : NULL;
$reback_url_str   = (isset($reback_url) && !empty($reback_url)) ? '&reback_url='.urlencode($reback_url) : NULL;
$lang_str         = '&lang='.$lang;
$oauth_token_str  = (isset($access_token) && !empty($access_token)) ? '&oauth_token='.$access_token : NULL;
$cancel_url       = $base_server_path.OAuthConfig::DEFAULT_AJAX_URL.'?item=logout'.$call_back_str.$reback_url_str.$lang_str.$oauth_token_str;

//setting the form datas into array.
$form = array();
$using_real_env               = OAuthModelConfig::USING_TEMPLATE_ON_REAL_ENV;
$page                         = OAuthModelConfig::M_PAGE_OF_CPLAN;
$form['lang']                 = $lang;
$form['title']                = $choose_plan_page[$lang]['page_title'];
$form['cplan_title']          = $choose_plan_page[$lang]['choose_plan_title'];
$form['cplan_note']           = $choose_plan_page[$lang]['choose_plan_txt'];
$form['cplan_null']           = $choose_plan_page[$lang]['no_plans_showing'];
$form['package_lists']        = $package_list;
$form['button']['cancel']     = array('value'=>$payment_buttons[$lang]['cancel'],'link'=>$cancel_url);
$form['a_step']               = 'one';
$form['from_local']           = false;
$form['is_logined']           = $is_logined;

//include thie template for plan cmodel page.
include "..".OAuthModelConfig::DEFAULT_MOBILE_TEMPLATE.OAuthModelConfig::$mobile_teplate_page[$page];

?>
